package com.fr.cell.core;

import com.fr.cell.Grid;
import com.fr.cell.GridSelection;
import com.fr.cell.ReportPane;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.accessibility.AccessibleContext;
import javax.swing.BoundedRangeModel;
import javax.swing.JScrollBar;

public class DynamicScrollBar extends JScrollBar
{
  private ReportPane reportPane;
  private boolean isSupportHide = true;

  public DynamicScrollBar(int paramInt, ReportPane paramReportPane)
  {
    super(paramInt);
    this.reportPane = paramReportPane;
    setMinimum(0);
    setUnitIncrement(1);
    setBlockIncrement(3);
    addComponentListener(new ComponentListener(this)
    {
      private final DynamicScrollBar this$0;

      public void componentResized()
      {
        ajustValues();
      }

      public void componentMoved()
      {
        ajustValues();
      }

      public void componentShown()
      {
        ajustValues();
      }

      public void componentHidden()
      {
        ajustValues();
      }

      private void ajustValues()
      {
        this.this$0.setValue(this.this$0.getValue());
      }
    });
  }

  public ReportPane getReportPane()
  {
    return this.reportPane;
  }

  public void setReportPane(ReportPane paramReportPane)
  {
    if (paramReportPane == null)
      return;
    this.reportPane = paramReportPane;
    if (this.orientation == 1)
      setVerticalValue(this.reportPane.getGrid().getVerticalValue());
    else
      setHorizontalValue(this.reportPane.getGrid().getHorizontalValue());
  }

  public int getVisibleAmount()
  {
    if (this.reportPane == null)
      return 0;
    if (this.orientation == 1)
      return getVerticalExtent(getValue());
    return getHorizontalExtent(getValue());
  }

  public void setValue(int paramInt)
  {
    if (this.reportPane == null)
      return;
    if (this.orientation == 1)
      setVerticalValue(paramInt);
    else
      setHorizontalValue(paramInt);
  }

  private void setVerticalValue(int paramInt)
  {
    int l;
    int i1;
    int i2;
    int i = getValue();
    TemplateReport localTemplateReport = this.reportPane.getEditingReport();
    DynamicValueList localDynamicValueList = ReportHelper.getRowHeightList(localTemplateReport);
    int j = localTemplateReport.getRowCount() - 1;
    if (paramInt < 0)
      paramInt = 0;
    if (i < paramInt)
    {
      k = paramInt - i;
      l = 0;
      i1 = i + 1;
      while (true)
      {
        if ((localDynamicValueList.get(i1) > 0) && (++l == k))
        {
          paramInt = i1;
          break label140:
        }
        ++i1;
      }
    }
    if (i > paramInt)
    {
      k = i - paramInt;
      l = 0;
      for (i1 = i - 1; i1 >= 0; --i1)
        if ((localDynamicValueList.get(i1) > 0) && (++l == k))
        {
          paramInt = i1;
          break;
        }
    }
    label140: int k = getVerticalExtent(paramInt);
    if (!(this.reportPane.isRowEndless()))
    {
      l = j + 1;
      i1 = l;
      if ((paramInt > i1 - k) && (paramInt > i))
      {
        i2 = getVerticalExtent(i);
        if (i >= i1 - i2)
          paramInt = i;
      }
    }
    else
    {
      l = Math.max(j, k) + 1;
      l = Math.max(paramInt + k, l);
      localObject = this.reportPane.getGridSelection();
      i2 = ((GridSelection)localObject).getType();
      if (i2 == 0)
      {
        Rectangle localRectangle = ((GridSelection)localObject).getFirstCellRectangle();
        l = Math.max(localRectangle.y + localRectangle.height + 1, l);
      }
    }
    Object localObject = getModel();
    if ((paramInt != ((BoundedRangeModel)localObject).getValue()) || (k != ((BoundedRangeModel)localObject).getExtent()) || (l != ((BoundedRangeModel)localObject).getMaximum()))
    {
      if (!(this.reportPane.isRowEndless()))
        ((BoundedRangeModel)localObject).setRangeProperties(paramInt, k, 0, l, false);
      else
        ((BoundedRangeModel)localObject).setRangeProperties(paramInt, k, 0, l, true);
      this.reportPane.getGrid().setVerticalValue(paramInt);
      this.reportPane.getGrid().setVerticalExtent(k);
      this.reportPane.repaint(40L);
      if (this.accessibleContext != null)
        this.accessibleContext.firePropertyChange("AccessibleValue", new Integer(i), new Integer(getValue()));
    }
  }

  private int getVerticalExtent(int paramInt)
  {
    TemplateReport localTemplateReport = this.reportPane.getEditingReport();
    DynamicValueList localDynamicValueList = ReportHelper.getRowHeightList(localTemplateReport);
    double d1 = this.reportPane.getGrid().getHeight();
    double d2 = 0.0D;
    int i = paramInt;
    int j = 2147483647;
    for (int k = i; k <= j; ++k)
    {
      d2 += localDynamicValueList.get(k);
      if (d2 > d1)
      {
        i = k;
        int l = k;
        while (true)
        {
          if (localDynamicValueList.get(l) != 0)
            break label100;
          i = l;
          ++l;
        }
      }
    }
    label100: return (i - paramInt);
  }

  private void setHorizontalValue(int paramInt)
  {
    int l;
    int i1;
    int i2;
    int i = getValue();
    TemplateReport localTemplateReport = this.reportPane.getEditingReport();
    DynamicValueList localDynamicValueList = ReportHelper.getColumnWidthList(localTemplateReport);
    int j = this.reportPane.getEditingReport().getColumnCount() - 1;
    if (paramInt < 0)
      paramInt = 0;
    if (i < paramInt)
    {
      k = paramInt - i;
      l = 0;
      i1 = i + 1;
      while (true)
      {
        if ((localDynamicValueList.get(i1) > 0) && (++l == k))
        {
          paramInt = i1;
          break label146:
        }
        ++i1;
      }
    }
    if (i > paramInt)
    {
      k = i - paramInt;
      l = 0;
      for (i1 = i - 1; i1 >= 0; --i1)
        if ((localDynamicValueList.get(i1) > 0) && (++l == k))
        {
          paramInt = i1;
          break;
        }
    }
    label146: int k = getHorizontalExtent(paramInt);
    if (!(this.reportPane.isColumnEndless()))
    {
      l = j + 1;
      i1 = l;
      if ((paramInt > i1 - k) && (paramInt > i))
      {
        i2 = getHorizontalExtent(i);
        if (i >= i1 - i2)
          paramInt = i;
      }
    }
    else
    {
      l = Math.max(j, k) + 1;
      l = Math.max(paramInt + k, l);
      localObject = this.reportPane.getGridSelection();
      i2 = ((GridSelection)localObject).getType();
      if (i2 == 0)
      {
        Rectangle localRectangle = ((GridSelection)localObject).getFirstCellRectangle();
        l = Math.max(localRectangle.x + localRectangle.width + 1, l);
      }
    }
    Object localObject = getModel();
    if ((paramInt != ((BoundedRangeModel)localObject).getValue()) || (k != ((BoundedRangeModel)localObject).getExtent()) || (l != ((BoundedRangeModel)localObject).getMaximum()))
    {
      if (!(this.reportPane.isColumnEndless()))
        ((BoundedRangeModel)localObject).setRangeProperties(paramInt, k, 0, l, false);
      else
        ((BoundedRangeModel)localObject).setRangeProperties(paramInt, k, 0, l, true);
      this.reportPane.getGrid().setHorizontalValue(paramInt);
      this.reportPane.getGrid().setHorizontalExtent(k);
      this.reportPane.repaint(10L);
      if (this.accessibleContext != null)
        this.accessibleContext.firePropertyChange("AccessibleValue", new Integer(i), new Integer(getValue()));
    }
  }

  private int getHorizontalExtent(int paramInt)
  {
    int i;
    TemplateReport localTemplateReport = this.reportPane.getEditingReport();
    DynamicValueList localDynamicValueList = ReportHelper.getColumnWidthList(localTemplateReport);
    double d1 = this.reportPane.getGrid().getWidth();
    double d2 = 0.0D;
    int j = paramInt;
    while (true)
    {
      d2 += localDynamicValueList.get(j);
      if (d2 > d1)
      {
        i = j;
        int k = j;
        while (true)
        {
          if (localDynamicValueList.get(k) != 0)
            break label85;
          i = k;
          ++k;
        }
      }
      ++j;
    }
    label85: return (i - paramInt);
  }

  public boolean isSupportHide()
  {
    return this.isSupportHide;
  }

  public void setSupportHide(boolean paramBoolean)
  {
    this.isSupportHide = paramBoolean;
  }

  public Dimension getPreferredSize()
  {
    if (this.isSupportHide)
    {
      if (getOrientation() == 0)
      {
        if ((this.reportPane == null) || (this.reportPane.isHorizontalScrollBarVisible()))
          break label76;
        return new Dimension(0, 0);
      }
      if ((getOrientation() == 1) && (this.reportPane != null) && (!(this.reportPane.isVerticalScrollBarVisible())))
        return new Dimension(0, 0);
    }
    label76: return super.getPreferredSize();
  }
}